package com.jiezhuo.controller.jw;


import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.read.builder.ExcelReaderBuilder;
import com.alibaba.excel.read.metadata.ReadSheet;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.jiezhuo.config.Config;
import com.jiezhuo.easyExcel.entity.*;
import com.jiezhuo.easyExcel.listen.DomeListen;
import com.jiezhuo.easyExcel.service.ServiceBean;
import com.jiezhuo.easyExcel.service.imp.StuExcelServiceImp;
import com.jiezhuo.entity.LoginUser;
import com.jiezhuo.entity.jw.JwVo;
import com.jiezhuo.entity.jw.JwXqKskcStuFb;
import com.jiezhuo.entity.jw.StuVo;
import com.jiezhuo.entity.jzg.JzgVO;
import com.jiezhuo.mapper.center.JwXqStudentMapper;
import com.jiezhuo.service.jw.JwXqKskcStuFbService;
import com.jiezhuo.service.jw.JwXqStudentService;
import com.jiezhuo.service.jzg.JzgNewMenuService;
import com.jiezhuo.service.jzg.TJzgService;
import com.jiezhuo.utils.R;
import com.jiezhuo.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;

@RestController
@Api(tags = "学生Excel")
@RequestMapping("/T-stuExcel")
public class TstuExcelController {
    @Autowired
    Config config;

    @Autowired
    TJzgService tJzgService;

    @Autowired
    JzgNewMenuService jzgNewMenuService;

    @Autowired
    JwXqKskcStuFbService jwXqKskcStuFbService;
    @Autowired
    StuExcelServiceImp stuExcelServiceImp;
    @Autowired
    JwXqStudentMapper jwXqStudentMapper;


    @ApiOperation("导入学生分班信息")
    @PostMapping("exportStuInfo")
    public R  toLeadStuInfo(MultipartFile file) throws IOException {


        DomeListen domeListen = new DomeListen(stuExcelServiceImp);

        try (ExcelReader excelReader = EasyExcel.read(file.getInputStream(), HeardExcel.class,domeListen ).build()) {
            // 构建一个sheet 这里可以指定名字或者no
            ReadSheet readSheet = EasyExcel.readSheet(1).head( StuFbExcel.class).build();
            System.out.println(readSheet);
            // 读取一个sheet
            excelReader.read(readSheet);
        }

        return R.ok();
    }
    @ApiOperation("导出考试学生")
    @GetMapping("exportStuKsInfo")
    public R  exportStuKsInfo(StuVo s) throws IOException {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        LoginUser loginUser = (LoginUser) authentication.getPrincipal();
        String userId=loginUser.getUser().getUserid();
        R userPermissionInfo = jzgNewMenuService.getUserPermissionInfo(userId);

        if(ObjectUtils.isEmpty(s.getXqdm())){
            s.setXqdm(userPermissionInfo.getData().get("schools").toString());
        }else {
            Set<String> collect = Arrays.stream(userPermissionInfo.getData().get("schools").toString().split(",")).collect(Collectors.toSet());
            boolean contains = collect.contains("'" + s.getXqdm() + "'");
            if (!contains){
                s.setXqdm("'not'");
            }else {
                s.setXqdm("'" + s.getXqdm() + "'");
            }
        }
        ArrayList<StuKsExcel> arrayList = jwXqStudentMapper.selectKsStudentList(s);
        String projectPath = System.getProperty("user.dir");
        String fileName =   "/考试学生名单" + System.currentTimeMillis() + ".xlsx";
        HeardExcel heardExcel = new HeardExcel();
        heardExcel.setData("注意：导出数据放在第二张开始的Sheet");
        ArrayList<HeardExcel> easyExcels = new ArrayList<>();
        easyExcels.add(heardExcel);
        ExcelWriter excelWriter = EasyExcel.write(projectPath+fileName, HeardExcel.class).build();
        WriteSheet writeSheet = EasyExcel.writerSheet(0, "sheet" + 1).build();
        WriteSheet writeSheet2 = EasyExcel.writerSheet(1, "导入模板" ).head(StuKsExcel.class).build();
        excelWriter.write(easyExcels, writeSheet);
        excelWriter.write(arrayList,writeSheet2);
        excelWriter.finish();
        excelWriter.close();
        return R.ok().data("url","http://10.176.132.35:8091/getExcel"+fileName);
    }

    @ApiOperation("获取学校学生数据")
    @GetMapping("exportStuInfo")
    public R  exportStuInfo(StuVo jzgVO) throws IOException {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        LoginUser loginUser = (LoginUser) authentication.getPrincipal();
        String userId=loginUser.getUser().getUserid();
        R userPermissionInfo = jzgNewMenuService.getUserPermissionInfo(userId);

        if(ObjectUtils.isEmpty(jzgVO.getXqdm())){
            jzgVO.setXqdm(userPermissionInfo.getData().get("schools").toString());
        }else {
            Set<String> collect = Arrays.stream(userPermissionInfo.getData().get("schools").toString().split(",")).collect(Collectors.toSet());
            boolean contains = collect.contains("'" + jzgVO.getXqdm() + "'");
            if (!contains){
                jzgVO.setXqdm("'not'");
            }
        }
        String projectPath = System.getProperty("user.dir");
        List<StuFbExcel> list=jwXqKskcStuFbService.getStuExcel(jzgVO);
        List<BjExcel> listbj=jwXqKskcStuFbService.getStuBjExcel(jzgVO);
        HeardExcel heardExcel = new HeardExcel();
        heardExcel.setData("注意：导出数据放在第二张开始的Sheet");
        ArrayList<HeardExcel> easyExcels = new ArrayList<>();
        easyExcels.add(heardExcel);
        String fileName =   "/学生信息分班导入" + System.currentTimeMillis() + ".xlsx";
        ExcelWriter excelWriter = EasyExcel.write(projectPath+fileName, HeardExcel.class).build();
        WriteSheet writeSheet = EasyExcel.writerSheet(0, "sheet" + 1).build();
        WriteSheet writeSheet2 = EasyExcel.writerSheet(1, "导入模板" ).head(StuFbExcel.class).build();
        WriteSheet writeSheet3 = EasyExcel.writerSheet(2, "班级代码" ).head(BjExcel.class).build();
        excelWriter.write(easyExcels, writeSheet);
        excelWriter.write(list,writeSheet2);
        excelWriter.write(listbj,writeSheet3);
        excelWriter.finish();
        excelWriter.close();
        return R.ok().data("usl","http://10.176.132.35:8091/getExcel"+fileName);
    }
    @ApiOperation("导出在学校走班学生数据")
    @GetMapping("exportZbStuInfo")
    public R  exportZbStuInfo(StuVo jzgVO) throws IOException {
        if(ObjectUtils.isEmpty(jzgVO.getNj())){
            return R.err().msg("请选择年级");
        }
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        LoginUser loginUser = (LoginUser) authentication.getPrincipal();
        String userId=loginUser.getUser().getUserid();
        R userPermissionInfo = jzgNewMenuService.getUserPermissionInfo(userId);
        if(ObjectUtils.isEmpty(jzgVO.getXqdm())){
            jzgVO.setXqdm(userPermissionInfo.getData().get("schools").toString());
        }else {
            Set<String> collect = Arrays.stream(userPermissionInfo.getData().get("schools").toString().split(",")).collect(Collectors.toSet());
            boolean contains = collect.contains("'" + jzgVO.getXqdm() + "'");
            if (!contains){
                jzgVO.setXqdm("'not'");
            }
        }
        String projectPath = System.getProperty("user.dir");
        List<StuZbFbExcel> list=jwXqKskcStuFbService.getStuZbExcel(jzgVO);
        List<ZbBjExcel> listbj=jwXqKskcStuFbService.getStuBjZbExcel(jzgVO);
        HeardExcel heardExcel = new HeardExcel();
        heardExcel.setData("注意：导出数据放在第二张开始的Sheet");
        ArrayList<HeardExcel> easyExcels = new ArrayList<>();
        easyExcels.add(heardExcel);
        String fileName =   "/学生信息分班导入" + System.currentTimeMillis() + ".xlsx";
        ExcelWriter excelWriter = EasyExcel.write(projectPath+fileName, HeardExcel.class).build();
        WriteSheet writeSheet = EasyExcel.writerSheet(0, "sheet" + 1).build();
        WriteSheet writeSheet2 = EasyExcel.writerSheet(1, "导入模板" ).head(StuZbFbExcel.class).build();
        WriteSheet writeSheet3 = EasyExcel.writerSheet(2, "班级代码" ).head(ZbBjExcel.class).build();
        excelWriter.write(easyExcels, writeSheet);
        excelWriter.write(list,writeSheet2);
        excelWriter.write(listbj,writeSheet3);
        excelWriter.finish();
        excelWriter.close();
        return R.ok().data("usl","http://10.176.132.35:8091/getExcel"+fileName);
    }




}
